package com.cndw.kungfu.model.message;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.domain.Event;
import com.cndw.kungfu.domain.Mail;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.domain.SubPops;
import com.cndw.kungfu.vo.PlayerReceiveVO;

/**
 * 短信服务
 * 
 * @author fantadust
 * @date 2011-5-11 下午04:11:44
 */
@Component
public class MessageServer {
	@Autowired
	private MailManager mailManager;
	@Autowired
	private EventManager eventManager;

	/**
	 * 发送系统信件
	 */
	public void sendSysMail(long playerId, String playerName, String title, String content) {
		sendMail(playerId, playerName, -1, "", title, content, Mail.SYS, null);
	}

	public void sendSysMail(long playerId, String playerName, String title, String content, List<SubPops> attachment) {
		sendMail(playerId, playerName, -1, "", title, content, Mail.SYS, attachment);
	}

	/**
	 * 发送信件
	 */
	public void sendMail(long playerId, String playerName, long senderId, String senderName, String title, String content) {
		sendMail(playerId, playerName, senderId, senderName, title, content, Mail.USER, null);
	}

	/**
	 * 发送信件
	 */
	private void sendMail(long playerId, String playerName, long senderId, String senderName, String title, String content, int type,
			List<SubPops> attachment) {
		Mail mail = new Mail();
		mail.setPlayerId(playerId);
		mail.setPlayerName(playerName);
		mail.setSenderId(senderId);
		mail.setSenderName(senderName);
		mail.setTitle(title);
		mail.setContent(content);
		mail.setAddTime(TimeProcesser.getUnixTime());

		if (!(BlankUtils.isBlank(attachment) || attachment.isEmpty())) {
			try {
				mail.setAttachment(JsonProcesser.objectJsonEncode(attachment));
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		int id = mailManager.newMail(mail);
		mail.setId(id);

		if (id > 0) {
			Context.getPlayerServer().receive(PlayerReceiveVO.RECEIVE_MAIL, id, playerId);
		}

	}

	/**
	 * 发送事件
	 * 
	 * @param playerId
	 */
	public void recordEvent(long playerId, String title, String content, int type) {
		Event event = new Event();
		event.setPlayerId(playerId);
		event.setTitle(title);
		event.setContent(content);
		event.setType(type);
		event.setAddTime(TimeProcesser.getUnixTime());
		int id = eventManager.newEvent(event);
		event.setId(id);
		if (id > 0) {
			Context.getPlayerServer().receive(PlayerReceiveVO.RECEIVE_EVENT, id, playerId);
		}

	}

	public void recordEvent(final long playerId, final String title, final String content, final int type, int delay) {
		if (delay <= 0) {
			recordEvent(playerId, title, content, type);
			return;
		}
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				recordEvent(playerId, title, content, type);
			}
		};
		Context.getTimerServer().scheduleSeconds(runnable, delay);

	}
	

	/**
	 * 清理邮件和事件
	 */
	public void clearMessageData() {
		eventManager.clearData();
		mailManager.clearData();

	}

}
